﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.ComponentModel;

namespace SalaryChannel.Web
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    partial class PublicInfor : ServiceAuthRoot, IPublicInfor
    {
        SalaryChannel.Web.DataClassesQueryDataContext context = new DataClassesQueryDataContext();
       
        
        public List<PublicInfor> GetPublicInfor()
        {
            try
            {
                Authoricate();
                DateTime now = DateTime.Now;
                var query = from p in context.PublicInfor
                            where now <= p.Expire_Date
                            where p.IsValid == '1'
                            orderby p.Create_Date descending
                            select p;

                return query.ToList<PublicInfor>();
            }
            catch (Exception ex)
            {
                LogManager.Write("获取公告信息失败:" + ex.Message + "--" + HttpContext.Current.Request.UserHostAddress);
                return new List<PublicInfor>();
            }

        }
        
        public bool ExposePublicInfor(int ID)
        {
            try
            {
                Authoricate();
                var query = from p in context.PublicInfor
                            where p.ID == ID
                            select p;

                PublicInfor pi = query.First();
                pi.IsValid = '0';
                //context.PublicInfor.u(pi);
                context.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.Write("ExposePublicInfor: " + ID.ToString() + "," + ex.Message);
                return false; ;
            }
        }


        // 在此处添加更多操作并使用  标记它们
        
        public bool AddPublicInofr(string Body, string ExpireDate, string UserID)
        {
            try
            {
                Authoricate();
                var query = from q in context.UserInfor
                            where q.User_ID == UserID
                            select q.UserName;


                PublicInfor pi = new PublicInfor();
                pi.Body = string.Format("{0} \r[{1}  {2}]", Body, query.First(), DateTime.Now.ToShortDateString());
                pi.Create_Date = DateTime.Now;
                pi.IsValid = '1';
                pi.Expire_Date = (DateTime)(new DateTimeConverter().ConvertFromString(ExpireDate));

                context.PublicInfor.InsertOnSubmit(pi);
                context.SubmitChanges();

               
                return true;
            }
            catch (Exception ex)
            {
                LogManager.Write("AddPublicInofr: "+ ex.Message);
                return false;
            }

        }
    }
}
